package com.zhongkun.datahome.service.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongkun.datahome.service.constant.EntityTypeEnum;
import com.zhongkun.datahome.service.constant.YesOrNoEnum;
import com.zhongkun.datahome.service.domain.CommonFile;
import com.zhongkun.datahome.service.mapper.CommonFileMapper;
import com.zhongkun.datahome.service.utils.StreamUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lifengsheng
 * @Date: 2025/4/14 17:15
 * @Desc
 */
@Repository
public class CommonFileRepository extends ServiceImpl<CommonFileMapper, CommonFile> {
    public List<CommonFile> listBySkuId(String skuId, String fileType) {
        return this.list(new LambdaQueryWrapper<CommonFile>()
                .eq(CommonFile::getEntityId, skuId)
                .eq(CommonFile::getEntityType, EntityTypeEnum.SKU_FILE.getType())
                .eq(CommonFile::getFileType, fileType));
    }

    public Map<String, CommonFile> getSkuHeadPicsMap(List<String> skuIds, String fileType) {
        if (CollectionUtils.isEmpty(skuIds)) {
            return Collections.emptyMap();
        }

        List<CommonFile> list = this.list(new LambdaQueryWrapper<CommonFile>()
                .in(CommonFile::getEntityId, skuIds)
                .eq(CommonFile::getEntityType, EntityTypeEnum.SKU_FILE.getType())
                .eq(CommonFile::getFileType, fileType)
                .eq(CommonFile::getIsHead, YesOrNoEnum.YES.getCode()));
        return StreamUtil.listToMap(list, CommonFile::getEntityId, e -> e);
    }

    public Map<String, List<String>> getSkuPicUrlsMap(List<String> skuIds, String fileType) {
        if (CollectionUtils.isEmpty(skuIds)) {
            return Collections.emptyMap();
        }

        List<CommonFile> list = this.list(new LambdaQueryWrapper<CommonFile>()
                .select(CommonFile::getEntityId, CommonFile::getFileUrl, CommonFile::getSort)
                .in(CommonFile::getEntityId, skuIds)
                .eq(CommonFile::getEntityType, EntityTypeEnum.SKU_FILE.getType())
                .eq(CommonFile::getFileType, fileType));
        return StreamUtil.listGroupByAndThen(list, null, CommonFile::getEntityId, e -> e.stream()
                .sorted(Comparator.comparingInt(CommonFile::getSort))
                .map(CommonFile::getFileUrl)
                .filter(StringUtils::isNotBlank)
                .collect(Collectors.toList()));
    }

    public void removeBySkuId(List<String> skuIds, String fileType) {
        if (CollectionUtils.isEmpty(skuIds)) {
            return;
        }

        this.remove(new LambdaQueryWrapper<CommonFile>()
                .in(CommonFile::getEntityId, skuIds)
                .eq(CommonFile::getEntityType, EntityTypeEnum.SKU_FILE.getType())
                .eq(CommonFile::getFileType, fileType));
    }

    public void removeByAssetsId(List<String> assetsIds) {
        if (CollectionUtils.isEmpty(assetsIds)) {
            return;
        }

        this.remove(new LambdaQueryWrapper<CommonFile>()
                .in(CommonFile::getEntityId, assetsIds)
                .eq(CommonFile::getEntityType, EntityTypeEnum.ASSETS_FILE.getType()));
    }

    public List<CommonFile> listByDataAssetsIds(List<String> dataAssetsIds, EntityTypeEnum entityTypeEnum) {
        if (CollectionUtils.isEmpty(dataAssetsIds)) {
            return Collections.emptyList();
        }

        return this.list(new LambdaQueryWrapper<CommonFile>()
                .eq(CommonFile::getEntityType, entityTypeEnum.getType())
                .in(CommonFile::getEntityId, dataAssetsIds));
    }
}
